var app = getApp();

Page({
    data: {},
    onLoad: function(o) {
        var a = this;
        a.setData({
            img_height: o.img_height,
            img_width: o.img_width,
            proportion: o.proportion
        }), wx.getSystemInfo({
            success: function(t) {
                a.setData({
                    width: t.windowWidth,
                    height: t.windowHeight
                });
            }
        }), wx.setNavigationBarColor({
            frontColor: "#ffffff",
            backgroundColor: wx.getStorageSync("color"),
            animation: {
                duration: 0,
                timingFunc: "easeIn"
            }
        }), app.util.request({
            url: "entry/wxapp/System",
            cachetime: "0",
            success: function(t) {
                console.log(t), a.setData({
                    system: t.data
                });
            }
        }), app.util.request({
            url: "entry/wxapp/Url2",
            cachetime: "0",
            success: function(t) {
                wx.downloadFile({
                    url: t.data + o.qr_code,
                    success: function(t) {
                        console.log(t.tempFilePath);
                        var e = t.tempFilePath;
                        a.setData({
                            qr_code: e
                        }), app.util.request({
                            url: "entry/wxapp/PostInfo",
                            cachetime: "0",
                            data: {
                                id: o.post_info_id
                            },
                            success: function(t) {
                                console.log(t), "" != t.data.tz.img ? (t.data.tz.img = t.data.tz.img.split(","), 
                                a.setData({
                                    post: t.data.tz
                                }), a.logo()) : (a.setData({
                                    post: t.data.tz
                                }), a.canvas());
                            }
                        });
                    }
                });
            }
        }), app.util.request({
            url: "entry/wxapp/Url",
            cachetime: "0",
            success: function(t) {
                a.setData({
                    url: t.data
                });
            }
        });
    },
    logo: function(t) {
        var o = this, e = o.data;
        console.log(e), wx.downloadFile({
            url: e.url + e.post.img[0],
            success: function(t) {
                console.log(t.tempFilePath);
                var e = t.tempFilePath;
                o.setData({
                    logo: e
                }), o.canvas();
            }
        });
    },
    canvas: function(t) {
        var e = this, o = (r = e.data).logo, a = (r.width, r.height, r.width - 40), s = r.proportion, i = e.data.post.details.split(""), n = "", l = [], c = wx.createCanvasContext("firstCanvas");
        c.setFontSize(14), c.setFillStyle("#000");
        for (var r = 0; r < i.length; r++) c.measureText(n).width < a ? n += i[r] : (l.push(n), 
        n = "");
        l.push(n), console.log(l);
        for (var h = 0; h < l.length; h++) c.fillText(l[h], 20, 120 + 20 * h, 700);
        var u = 140 + 20 * h;
        if (null != o) var p = 160 + 20 * h + a * s; else p = 160 + 20 * h;
        console.log(p), e.setData({
            canvas_height: p,
            text_height: u
        }), e.save_canvas();
    },
    save_canvas: function(t) {
        var e = this, o = (u = e.data).logo, a = u.width, s = u.proportion, i = u.width - 40, n = u.canvas_height, l = wx.createCanvasContext("firstCanvas");
        l.setFillStyle("#fff"), l.rect(0, 0, a, n), l.fill(), console.log(u.qr_code), console.log(u.post.type_name), 
        console.log(u.post.sh_time), console.log(u.post.details), console.log(u.logo), l.drawImage(u.qr_code, a - 120, 20, 100, 100), 
        l.fillStyle = "#000", l.setFontSize(18), l.fillText(e.data.post.type_name, 20, 40), 
        l.fillStyle = "#999", l.setFontSize(12), l.fillText(app.ormatDate(u.post.sh_time) + "发布", 20, 70), 
        l.fillText(u.post.views + "人浏览", 20, 90);
        var c = e.data.post.details.split(""), r = "", h = [];
        l.setFontSize(14), l.setFillStyle("#000");
        for (var u = 0; u < c.length; u++) l.measureText(r).width < i ? r += c[u] : (h.push(r), 
        r = "");
        h.push(r), console.log(h);
        for (var p = 0; p < h.length; p++) l.fillText(h[p], 20, 120 + 20 * p, 700);
        null != o && l.drawImage(o, 20, 140 + 20 * p, i, i * s), l.draw();
    },
    totemp: function(t) {
        var e = this.data.width, o = this.data.canvas_height;
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: e,
            height: o,
            canvasId: "firstCanvas",
            success: function(t) {
                console.log(t.tempFilePath), wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath,
                    success: function(t) {
                        console.log(t), wx.showToast({
                            title: "保存成功"
                        }), setTimeout(function() {
                            wx.navigateBack({
                                delta: 2
                            });
                        }, 1500);
                    },
                    fail: function(t) {},
                    complete: function(t) {}
                });
            }
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});